﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;

using OAPlatform.CommonHelper;
using OAPlatform.DomainModel.Model;

namespace OAPlatform.Test.Permission
{
    [TestFixture]
    class TestPermission
    {
        [SetUp]
        public void SetUp()
        {

        }

        /// <summary>
        /// 验证一个已经存在权限
        /// </summary>
        [Test]
        public void TestAuthenticateAExistedPermission()
        {
            string permissionKey = "testPermission";
            User user = new User { ID = 1, Name = "test" };
            user.Roles.Add(new Role());
            user.Roles[0].Add(new DomainModel.Model.Permission { ID = 1, Key = permissionKey });

            Assert.IsTrue(user.Authenticate(permissionKey));
        }

        /// <summary>
        /// 验证一个不存在的权限
        /// </summary>

        [Test]
        public void TestAuthenticateANotExistedPermission()
        {
            string permissionKey = "testNotExistedPermission";
            User user = new User { ID = 1, Name = "test" };

            Assert.IsFalse(user.Authenticate(permissionKey));
        }

        /// <summary>
        /// 验证从职位继承的权限
        /// </summary>

        [Test]
        public void TestAuthenticateUserPermissionInheirtFromBand()
        {
            string permissionKey = "testPermissionKey";
            string notExistedPermissionKey = "testNotExistedPermission";

            User user = new User { ID = 1, Name = "test" };
            user.Band.Roles.Add(new Role());
            user.Band.Roles[0].Add(new DomainModel.Model.Permission { ID = 1, Key = permissionKey });

            Assert.IsTrue(user.Authenticate(permissionKey));
            Assert.IsFalse(user.Authenticate(notExistedPermissionKey));
        }

        /// <summary>
        /// 验证从部分继承的权限
        /// </summary>

        [Test]
        public void TestAuthenticateUserPermissionInheirtFromDepartment()
        {
            string permissionKey = "testPermissionKey";
            string notExistedPermissionKey = "testNotExistedPermission";

            User user = new User { ID = 1, Name = "test" };
            user.Band.Department.Roles.Add(new Role());
            user.Band.Department.Roles[0].Add(new DomainModel.Model.Permission { ID = 1, Key = permissionKey });

            Assert.IsTrue(user.Authenticate(permissionKey));
            Assert.IsFalse(user.Authenticate(notExistedPermissionKey));
        }
    }
}
